package pers.wwz.study.file.utils;

import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;

public class JasyptUtils {

    public static StringEncryptor stringEncryptor(String password) {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(password);
        config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        return encryptor;
    }

    public static void main(String[] args) {

        String hrMysqlPassword = "Qetu#457adgh!";
        String feishuMysqlPassword = "sfk!85s_kafjWE";
        String redisPassword = "redispassword";
        StringEncryptor encryptor = JasyptUtils.stringEncryptor("zycfcHr");
        String hrMysqlEncrypt = encryptor.encrypt(hrMysqlPassword);
        System.out.println("HR数据库密码加密后:"+hrMysqlEncrypt);

        String hrMysqlDecrypt = encryptor.decrypt(hrMysqlEncrypt);
        System.out.println("HR数据库密码解密后:"+hrMysqlDecrypt);

        String feishuMysqlEncrypt = encryptor.encrypt(feishuMysqlPassword);
        System.out.println("飞书数据库密码加密后:"+feishuMysqlEncrypt);

        String feishuMysqlDecrypt = encryptor.decrypt(feishuMysqlEncrypt);
        System.out.println("飞书数据库密码解密后:"+feishuMysqlDecrypt);


        String redisEncrypt = encryptor.encrypt(redisPassword);
        System.out.println("redis密码加密后:"+redisEncrypt);

        String redisDecrypt = encryptor.decrypt(redisEncrypt);
        System.out.println("redis密码解密后:"+redisDecrypt);


    }
}
